<?php
    defined('INDEX') or die ('Acesso direto não permitido');
    
    class FormularioButtonBar
    {
        protected $formularioBaseInstance = null;
        
        public function __construct(FormularioBase $instance)
        {
            $this->formularioBaseInstance = $instance;
        }
        
        public function showButtonBar (stdClass $actions = null)
        {
            if(!$this->getFormularioBaseInstance()->isDisabledButtonBar())
            {
                if($actions == null)
                {
                    $actions = $this->_defaultObjectEventosPorAction();
                }
                
                $params = $this->getFormularioBaseInstance()->getSeoObject()->getObjectParams();
                
                if($actions->NEW)
                {
                    echo '<a href="index.php?pagina='.$params->pagina.'&amp;acao=novo">Novo</a><br/>';
                }
                if($actions->EDIT)
                {
                    echo '<a href="index.php?pagina='.$params->pagina.'&amp;acao=editar&amp;id='.$params->id.'">Editar</a><br/>';
                }
                if($actions->DELETE)
                {
                    echo '<a href="index.php?pagina='.$params->pagina.'&amp;acao=delete&amp;id='.$params->id.'">Excluir</a><br/>';
                }
                if($actions->SAVE)
                {
                    echo '<a href="index.php?pagina='.$params->pagina.'&amp;acao=salvar">Salvar</a><br/>';
                }
                if($actions->CANCEL)
                {
                    echo '<a href="index.php?pagina='.$params->pagina.'&amp;acao=lista">Listar</a><br/>';
                }
            }
        }
        
        private function _defaultObjectEventosPorAction ()
        {
            $actions = new stdClass();
            switch ($this->getFormularioBaseInstance()->getSeoObject()->getObjectParams()->acao)
            {
            	case 'novo':
            	    $actions->NEW     = false;
                    $actions->SAVE    = true;
                    $actions->EDIT    = false;
                    $actions->CANCEL  = true;
                    $actions->DELETE  = false;
            	break;
            	default:
            	case 'lista':
            	    $actions->NEW     = true;
                    $actions->SAVE    = false;
                    $actions->EDIT    = false;
                    $actions->CANCEL  = false;
                    $actions->DELETE  = false;
            	break;
            	case 'delete':
            	    $actions->NEW     = true;
                    $actions->SAVE    = false;
                    $actions->EDIT    = false;
                    $actions->CANCEL  = true;
                    $actions->DELETE  = true;
            	break;
            	case 'editar':
            	    $actions->NEW     = true;
                    $actions->SAVE    = true;
                    $actions->EDIT    = false;
                    $actions->CANCEL  = true;
                    $actions->DELETE  = true;
            	break;
            	case 'salvar':
            	    $actions->NEW     = false;
                    $actions->SAVE    = false;
                    $actions->EDIT    = false;
                    $actions->CANCEL  = true;
                    $actions->DELETE  = false;
            	break;
            }
            
            return $actions;
        }
        
        /**
         * @return FormularioBase $instance
         */
		public function getFormularioBaseInstance()
        {
            return $this->formularioBaseInstance;
        }
    }
?>